<template>
    <div class="container">
        <div class="scrm" v-for="item in list" :key="item.applicationId">
            <el-image class="img" :src="item.img" fit="fill"></el-image>
            <div class="title">{{ item.name }}<span v-if="item.state === 2">已开通</span></div>
            <span class="tip">{{ item.desc }}</span>
            <div class="footer" v-auth="'scrm:open'">
                <el-button
                    v-if="item.state === 1"
                    class="btn"
                    type="primary"
                    @click="submitHandler(item.applicationId, item.name)"
                    >马上开通</el-button
                >
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted, inject } from 'vue';
import request from '@/request';
import { ElMessage } from 'element-plus';
const confirm = inject('confirm');

const list = reactive([]);

const submitHandler = async (applicationId, name) => {
    if (await confirm(`确认开通${name}？开通后需至我的应用中进行系统配置！`)) {
        request('applicationOpen', { applicationId }).then(res => {
            ElMessage.success('操作成功');
            init(true);
        });
    }
};
onMounted(() => {
    init();
});
const init = async (type = false) => {
    const { data } = await request('applicationList');
    type && (list.length = 0);
    if (data && data.length !== 0) {
        data.map(item => list.push(item));
    }
};
</script>
<style lang="scss" scoped>
.container {
    padding-top: 12px;
    display: flex;
    flex-wrap: wrap;
}
.scrm {
    width: 432px;
    height: 362px;
    margin-right: 24px;
    margin-bottom: 24px;
    background: #ffffff;
    box-shadow: 0px 4px 4px 0px rgba(240, 240, 240, 0.5);
    border: 1px solid #eaeaea;
    display: flex;
    flex-direction: column;
    .img {
        width: 432px;
        height: 178px;
        margin-bottom: 18px;
    }
    .title {
        display: flex;
        align-items: center;
        font-size: 18px;
        font-weight: 500;
        color: #33302d;
        line-height: 18px;
        margin-bottom: 10px;
        padding-left: 24px;
        span {
            width: 64px;
            height: 22px;
            background: #f2fee5;
            border-radius: 2px;
            font-size: 12px;
            font-weight: 400;
            color: #60b900;
            line-height: 22px;
            text-align: center;
            margin-left: 8px;
        }
    }
    .tip {
        font-size: 14px;
        font-weight: 400;
        color: #848484;
        line-height: 20px;
        padding: 0 24px;
    }
    .footer {
        width: 100%;
        height: 96px;
        padding: 48px 24px 16px;
        box-sizing: border-box;
        display: flex;
        flex-direction: row-reverse;
        .btn {
            width: 80px;
            height: 32px;
        }
    }
}
</style>
